package com.comprehensive.dataservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comprehensive.common.dto.PageResponse;
import com.comprehensive.dataservice.common.dtoConverter.PageConverter;
import com.comprehensive.dataservice.entity.Novel;
import com.comprehensive.dataservice.entity.NovelChapter;
import com.comprehensive.dataservice.service.NovelChapterService;
import com.comprehensive.dataservice.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/novel")
public class NovelController {
    private final NovelChapterService novelChapterService;
    private final NovelService novelService;
    @Autowired
    public NovelController(NovelChapterService novelChapterService, NovelService novelService) {
        this.novelChapterService = novelChapterService;
        this.novelService = novelService;
    }
    @GetMapping("/listPage")
    public PageResponse<Novel> listPage(
            @RequestParam(defaultValue = "0") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        return PageConverter.convert(novelService.page(new Page<>(pageNum, pageSize)));
    }
    @GetMapping("/{id}")
    public Novel queryById(@PathVariable String id) {
        return novelService.getById(id);
    }
    @PostMapping
    public void save(@RequestBody Novel novel) {
        novelService.save(novel);
    }
    @PutMapping
    public void update(@RequestBody Novel novel) {
        novelService.updateById(novel);
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        novelService.removeById(id);
    }

    @GetMapping("/chapter/listPage")
    public PageResponse<NovelChapter> listChapterPage(
            @RequestParam(defaultValue = "0") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam String novelId
    ) {
        QueryWrapper<NovelChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("novel_id", novelId);
        queryWrapper.orderByAsc("sort");
        IPage<NovelChapter> page = novelChapterService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return PageConverter.convert(page);
    }
    @GetMapping("/chapter/list")
    public List<NovelChapter> listChapter(@RequestParam String novelId) {
        QueryWrapper<NovelChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("novel_id", novelId);
        queryWrapper.orderByAsc("sort");
        return novelChapterService.list(queryWrapper);
    }
    @GetMapping("/chapter/{id}")
    public NovelChapter queryChapterById(@PathVariable String id) {
        return novelChapterService.getById(id);
    }
    @PostMapping("/chapter")
    public void saveChapter(@RequestBody NovelChapter novelChapter) {
        novelChapterService.save(novelChapter);
    }
    @PutMapping("/chapter")
    public void updateChapter(@RequestBody NovelChapter novelChapter) {
        novelChapterService.updateById(novelChapter);
    }
    @DeleteMapping("/chapter/{id}")
    public void deleteChapter(@PathVariable String id) {
        novelChapterService.removeById(id);
    }
}
